import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import '../widgets/inputWidget.dart';
import '../helper/Helper.dart';
//import 'package:flutter_range_slider/flutter_range_slider.dart' as frs;

class TimeLinePanel extends StatefulWidget {
  @override
  TimeLinePanel(this.title, this.tList, this.vList, this.nn, this.xsn, this.dw);

  // final SectionX s1;
  String title;
  List<String> tList;
  List<double> vList;
  int nn;
  int xsn;
  String dw;

  @override
  _TimeLinePanelState createState() => _TimeLinePanelState();
}

class _TimeLinePanelState extends State<TimeLinePanel> {
  TextStyle _fText = new TextStyle(
    color: Color(0xff67727d),
    fontWeight: FontWeight.bold,
    fontSize: 15,
  );
  //SectionX X1;
  double _minX = 1000;
  double _maxX = 0;
  double _minY = 1000;
  double _maxY = 0;
  double _yD = 1;
  int _yFix = 0;

  late List<FlSpot> ALLData;

  var mainColor = const Color(0xff23b6e6);
  List<Color> gradientColors = [
    const Color(0xff23b6e6),
  ];

  bool showAvg = false;
  bool isLoaded = false;

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    if (!isLoaded) return new Container();

    return Column(children: [
      InputWidget.getGreyInfo2(widget.title, "单位:" + widget.dw),
      Stack(
        children: <Widget>[
          AspectRatio(
            aspectRatio: 1.70,
            child: Container(
              decoration: const BoxDecoration(
                  borderRadius: BorderRadius.all(
                    Radius.circular(3),
                  ),
                  color: Colors.white60),
              child: Padding(
                padding: const EdgeInsets.only(
                    right: 24.0, left: 12.0, top: 24, bottom: 12),
                child: LineChart(
                  mainData(),
                ),
              ),
            ),
          ),
        ],
      ),
    ]);
  }

  void _loadData() {
    ALLData = <FlSpot>[];
    double v1, v2;

    for (int i = 0; i < widget.nn; i++) {
      v1 = i * 1.0;
      v2 = widget.vList[i] * 1.0;
      var p1 = new FlSpot(v1, v2);
      ALLData.add(p1);
      if (v1 > _maxX) _maxX = v1;
      if (v1 < _minX) _minX = v1;
      if (v2 > _maxY) _maxY = v2;
      if (v2 < _minY) _minY = v2;
    }

    _minX = 0;
    if (_minY < 50) {
      _minY = 0;
    } else {
      _minY = Helper.RoundDown10(_minY, 10);
    }

    _maxX = (widget.nn - 1) * 1.0;

    _yD = getInterval(_minY, _maxY);
    _maxY = getmaxY(_minY, _maxY);
    _yFix = getFixY(_yD);

    setState(() {
      isLoaded = true;
    });
    //print([_maxY,_yD]);
  }

  double getLJ(double y1, y2) {
    if (y1 < 0.1 && y2 < 0.1) return 0.1;
    if (y1 < 1 && y2 < 1) return 1;
    if (y1 < 10 && y2 < 10) return 2;
    if (y1 < 50 && y2 < 50) return 5;
    if (y1 < 200 && y2 < 200) return 10;
    if (y2 > 1000) return 100;
    return 10;
  }

  double getInterval(double y1, y2) {
    if (y1 < 0.1 && y2 <= 0.1) return 0.05;
    if (y1 < 1 && y2 <= 1) return 0.2;
    if (y1 < 10 && y2 <= 10) return 2;
    if (y1 < 50 && y2 <= 50) return 5;
    if (y1 < 200 && y2 <= 200) return Helper.RoundUp10((y2 - y1) / 5.0, 10);
    if ((y2 - y1) < 10) return 1;
    if (y2 > 1000) return Helper.RoundUp10((y2 - y1) / 5.0, 100);
    return Helper.RoundUp10((y2 - y1) / 5.0, 10);
  }

  double getmaxY(double y1, y2) {
    var xx = getLJ(y1, y2);
    if (xx > 10) {
      if ((y2 - y1) < 10) xx = 10;
    }
    return Helper.RoundUp10(y2, xx);
  }

  int getFixY(double dd) {
    if (dd < 0.1) return 2;
    if (dd < 1) return 1;
    return 0;
  }

  LineChartData mainData() {
    return LineChartData(
      lineTouchData: LineTouchData(
        touchTooltipData: LineTouchTooltipData(
            tooltipBgColor: mainColor,
            getTooltipItems: (List<LineBarSpot> touchedBarSpots) {
              return touchedBarSpots.map((barSpot) {
                final flSpot = barSpot;
                return LineTooltipItem(
                  '${widget.tList[flSpot.x.toInt()]} , ${flSpot.y.toStringAsFixed(widget.xsn)} ',
                  const TextStyle(color: Colors.white),
                );
              }).toList();
            }),
      ),
      gridData: FlGridData(
        show: false,
      ),
      titlesData: FlTitlesData(
        show: true,
        bottomTitles: SideTitles(
          showTitles: true,
          getTitles: (value) {
            //xn++;
            if (value % 3 == 0 || value == 0)
              return widget.tList[value.round()];
            //xn++;
            return "";
          },
        ),
        topTitles: SideTitles(showTitles: false),
        leftTitles: SideTitles(
          showTitles: true,
          //getTextStyles: (value) => _fText,
          interval: _yD,
          getTitles: (value) {
            return value.toStringAsFixed(_yFix);
          },
          reservedSize: 28,
          margin: 12,
        ),
      ),
      borderData: FlBorderData(
          show: true,
          border: Border.all(color: const Color(0xff37434d), width: 1)),
      minX: _minX,
      maxX: _maxX,
      minY: _minY,
      maxY: _maxY,
      lineBarsData: [getALLData()],
    );
  }

  LineChartBarData getALLData() {
    return LineChartBarData(
      spots: ALLData,
      isCurved: true,
      colors: gradientColors,
      barWidth: 1,
      isStrokeCapRound: true,
      dotData: FlDotData(
        show: true,
      ),
      belowBarData: BarAreaData(
        show: false,
        colors: [Colors.brown],
      ),
    );
  }
}
